package vn.aptech.scrumgroup.model;

import java.util.ArrayList;

import vn.aptech.scrumgroup.util.DrawUtil;
import android.graphics.Canvas;
import android.graphics.ColorMatrix;
import android.graphics.ColorMatrixColorFilter;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.graphics.Rect;
import demo.axitdn.physic.Unit;

public class DailyScrumModel extends AbstractBaseModel{
	private int height;
	private int width;
	private float charWidth;
	ArrayList<PersonModel> list;
	Point startPoint;
	private Path path;
	
	float boundLeft;
	float boundRight;
	float boundTop;
	float boundBottom;
	
	public DailyScrumModel(Point startPoint, int width, int height, float charWidth, int backColor, int foreColor){
		super(backColor, foreColor);
		this.startPoint = startPoint;
		this.width = width;
		this.height = height;
		this.charWidth = charWidth;
		list = new ArrayList<PersonModel>();
		Point[] points = calPoint(startPoint);
		for(int i = 0 ; i < points.length; i++){
			PersonModel model = new PersonModel(points[i].x, points[i].y, width, height, backColor, foreColor);
			list.add(model);
		}
		
	}
	
	@Override
	public void onDraw(Canvas c) {
		// TODO Auto-generated method stub	
		Paint paint = new Paint();
		ColorMatrix cm = new ColorMatrix();
		int brightness = 120;
		if (isBrilliant){ 
			cm.set(new float[]{1,0,0,0,brightness,
		    		0,1,0,0,brightness,
		    		0,0,1,0,brightness,
		    		0,0,0,1,0
		    		});
			paint.setColorFilter(new ColorMatrixColorFilter(cm));
		}else {
			paint.setColorFilter(null);
		}//end else
		
		for(PersonModel model : list){
			model.setBrilliant(isBrilliant);
			model.onDraw(c);
		}		
		
		//position of Sprint Burndown
		int topS = startPoint.y + height/3;
		int bottomS = topS + height;
		int leftS = startPoint.x - width * 6;
		int rightS = startPoint.x - width * 4;
		int widthS = rightS - leftS;
		int heightS = bottomS - topS;
		boundLeft = leftS;
		boundBottom = startPoint.y + height * 3;
		
		//Sprint Burndown
		paint.setStyle(Style.FILL);
		
		path = new Path();
		path.moveTo(leftS, topS);
		path.lineTo(rightS, topS);
		path.lineTo(rightS, bottomS);
		path.lineTo(leftS, bottomS);
		path.lineTo(leftS, topS);
		c.drawPath(path, paint);
			
		//Sprint Burndown's line inside
		paint.setColor(backColor);
		paint.setAlpha(0);
		paint.setStyle(Style.STROKE);
		
		path = new Path();
		path.moveTo(leftS + Unit.i, topS + Unit.j);
		path.lineTo(rightS - widthS/3, bottomS - heightS/2);
		path.lineTo(rightS - Unit.i, bottomS - Unit.j);
		c.drawPath(path, paint);
		
		//draw text
		paint.setAlpha(255);
		//paint.setTextAlign(Align.CENTER);
		paint.setTextSize(DrawUtil.calculateTextSize("Daily Scrum", boundRight - boundLeft + width * Unit.i, paint, 3));
		drawText(c, "Daily Scrum", startPoint.x - 6 * width * Unit.i, startPoint.y + height * 3, paint, charWidth);
//		c.drawRect(boundLeft, boundTop, boundRight, boundBottom, paint);
	}

	@Override
	public void onUpdate(double t) {
		// TODO Auto-generated method stub
		
	}
	
	private Point[] calPoint(Point point){
		Point[] points = new Point[7];
		int radius = (int)((width * 5) / 8);
		int furtherWidth = (int)(width * 1.5f);
		
		points[0] = new Point(point.x, point.y);
		points[1] = new Point(point.x - furtherWidth, point.y + (radius * 2));
		points[2] = new Point(point.x + furtherWidth, point.y + (radius * 2));
		points[3] = new Point(points[1].x - width, points[1].y + (radius * 2));
		points[4] = new Point(points[2].x + width, points[2].y + (radius * 2));		
		points[5] = new Point(points[3].x + furtherWidth, points[3].y + (radius * 2));
		points[6] = new Point(points[4].x - furtherWidth, points[4].y + (radius * 2));
		
		boundTop = startPoint.y - height / 2 - radius * 2;
		//boundRight = startPoint.x + width / 2 + width / 9;
		boundRight = startPoint.x + width * 4;
		return points;
	}
	
	private void drawText(Canvas canvas, String text, float x, float y, Paint paint, float charWidth)
	{
		paint.setStyle(Style.FILL_AND_STROKE);
		if(charWidth > 0){
			paint.setStrokeWidth(charWidth);
		}
//		paint.setTextSize(height);
		paint.setAntiAlias(true);
		Rect r = new Rect();
		paint.getTextBounds(text, 0, text.length(), r);
		canvas.drawText(text, x, y + r.height()/2, paint);
	}
	

	@Override
	public boolean isPointOf(float x, float y) {
		// TODO Auto-generated method stub	
		if(x <= boundRight && x >= boundLeft)
			if(y <= boundBottom && y >= boundTop)
				return true;
		return false;
	}
	@Override
	public void changeColor(int color) {
		// TODO Auto-generated method stub
		
	}
}
